package com.qk.controller;

import com.qk.common.PageResult;
import com.qk.common.Result;
import com.qk.entity.Role;
import com.qk.service.RoleService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/roles")
public class RoleController {

    @Autowired
    private RoleService roleService;

    @PostMapping
    public Result add(@RequestBody Role role){
        log.info("新增角色:{}",role);
        System.out.println("添加角色:"+ role);
        roleService.add(role);
        return Result.success();
    }

    @GetMapping
    public Result PageList(String name,String label,
                           @RequestParam(defaultValue = "1") Integer Page,
                           @RequestParam(defaultValue = "10") Integer PageSize){
        log.info("条件分页查询角色:name={},label={},page={},pageSize={}",name,label,Page,PageSize);
        PageResult<Role> pageResult = roleService.listRoles(name,label,Page,PageSize);
        return Result.success(pageResult);
    }

    @GetMapping("/{id}")
    public Result getRoleById(@PathVariable Integer id){
        log.info("根据id查询角色:{}",id);
        System.out.println("查询id:" + id);
        Role role = roleService.getRoleById(id);
        return Result.success(role);
    }

    @PutMapping
    public Result updateRoles(@RequestBody Role role){
        log.info("修改角色:{}",role);
        roleService.updateRoles(role);
        return Result.success();
    }

    @RequestMapping("/{id}")
    public Result deleteById(@PathVariable Integer id){
        log.info("删除角色:{}",id);
        System.out.println("删除角色:" + id);
        roleService.deleteById(id);
        return Result.success();
    }

    @GetMapping("/list")
    public Result roleList(Role role){
        log.info("查询所有部门{}",role);
        List<Role> roleList = roleService.getRoleList(role);
        return Result.success(roleList);
    }
}
